package com.game.server.message.proto.execute;

import com.comm.ISession;

import com.game.majiang.config.DataManager;
import com.game.majiang.logic.LogicClient;
import com.game.majiang.logic.LogicServer;
import com.game.majiang.logic.LogicTable;
import com.game.server.db.dao.XyClubInfoDao;
import com.game.server.db.dao.XyUnionInfoDao;
import com.game.server.db.entity.UserInfoData;
import com.game.server.db.entity.XyClubInfo;
import com.game.server.message.proto.in.AbstractXYDissolveClubReq;
import com.game.server.message.proto.out.XYDissolveClubRsp;
import com.game.server.message.proto.out.XYInformDataAlterRsp;
import com.game.server.system.AbstractClient;
import com.game.server.system.AbstractRoom;
import com.game.server.system.AbstractTable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class XYDissolveClubReq extends AbstractXYDissolveClubReq {

    @Override
    public void Execute(ISession session) {
        LogicClient client = (LogicClient) LogicServer.getInstance().getClient(session.getId());
        if (client != null&&client.checkClick(msgcode)) {
            XYDissolveClubRsp rsp = new XYDissolveClubRsp();
            rsp.setIsok(1);
            rsp.setClubid(this.clubid);
            if (client.getClubid() == this.clubid){
                XyClubInfo xyClubInfo = DataManager.getInstance().getMapClub().get(this.clubid);
                if (xyClubInfo!=null && xyClubInfo.getCreatorid() == client.getUserId()){
                    rsp.setIsok(2);
                    if(xyClubInfo.getUnionid()==0){
                        rsp.setIsok(4);
                        if (xyClubInfo.getPlayerCount()==1){
                            rsp.setIsok(3);
                            boolean nextFlg = true;
                            Collection<AbstractRoom> rooms = LogicServer.getInstance().getRooms();
                            for (AbstractRoom room : rooms) {
                                ArrayList<AbstractTable> tables = room.getTables();
                                for (AbstractTable tempTable : tables) {
                                    LogicTable tab = (LogicTable) tempTable;
                                    if (tab.getClubid() == xyClubInfo.getId()){
                                        nextFlg = false;
                                        break;
                                    }
                                }
                            }
                            if (nextFlg){

                                XYInformDataAlterRsp alterRsp = new XYInformDataAlterRsp();
                                alterRsp.setType(3);
                                alterRsp.setOperateclubid(this.clubid);
                                alterRsp.setClubid(this.clubid);
                                alterRsp.setUnionid(0);
                                xyClubInfo.broadCastPlayer(alterRsp);

                                XyClubInfoDao.del(xyClubInfo.getId());
                                DataManager.getInstance().getMapClub().remove(Integer.valueOf(xyClubInfo.getId()));
                                HashMap<Integer, UserInfoData> userData = DataManager.getInstance().getMapUserData();
                                for (Map.Entry<Integer, UserInfoData> dataEntry : userData.entrySet()) {
                                    if (dataEntry.getValue().getClubids().contains(Integer.valueOf(xyClubInfo.getId()))){
                                        dataEntry.getValue().getClubids().remove(Integer.valueOf(xyClubInfo.getId()));
                                    }
                                }
                                rsp.setIsok(0);
//                                Collection<AbstractClient> clients = LogicServer.getInstance().getClients();
//                                for (AbstractClient abstractClient : clients) {
//                                    LogicClient client1 = (LogicClient) abstractClient;
//                                    if (client1.getClubid() == xyClubInfo.getId()){
//                                        client1.setClubid(0);
//                                    }
//                                }
                            }
                        }

                    }
                }
            }
            client.Send(rsp);
        }
    }

}
